package com.hzdesk.media.koreabatchmatvector.logic.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.DeviceAttributeBean;
import com.hzdesk.media.koreabatchmatvector.config.RBConfig;
import com.hzdesk.media.koreabatchmatvector.custom.FlyBorderView;
import com.hzdesk.media.koreabatchmatvector.custom.MyFlyBorderView;
import com.hzdesk.media.koreabatchmatvector.dao.DeviceAttributeBeanDaoUtils;
import com.hzdesk.media.koreabatchmatvector.logic.contract.MainContract;
import com.hzdesk.media.koreabatchmatvector.logic.persenter.MainPersenter;
import com.hzdesk.media.koreabatchmatvector.utils.DateUtil;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends BaseActivity implements MainContract.IMainView {

    @BindView(R.id.activity_my_main_iv_menu_logo)
    ImageView activityMyMainIvMenuLogo;
    @BindView(R.id.activity_my_main_civ_head)
    CircleImageView activityMyMainCivHead;
    @BindView(R.id.activity_my_main_name)
    TextView activityMyMainName;
    @BindView(R.id.activity_my_main_tv_state)
    TextView activityMyMainTvState;
    @BindView(R.id.activity_my_main_ll_menu_user)
    LinearLayout activityMyMainLlMenuUser;
    @BindView(R.id.activity_my_main_ll_meeting)
    LinearLayout activityMyMainLlMeeting;
    @BindView(R.id.activity_my_main_ll_local_video)
    LinearLayout activityMyMainLlLocalVideo;
    @BindView(R.id.activity_my_main_ll_lin_cloud)
    LinearLayout activityMyMainLlLinCloud;
    @BindView(R.id.activity_my_main_ll_lin_set)
    LinearLayout activityMyMainLlLinSet;
    @BindView(R.id.activity_my_main_ll_tv_time)
    TextView activityMyMainLlTvTime;
    @BindView(R.id.activity_my_main_tv_date)
    TextView activityMyMainTvDate;
    @BindView(R.id.activity_my_main_iv_network)
    ImageView activityMyMainIvNetWork;
    @BindView(R.id.activity_my_main_tv_net)
    TextView activityMyMainTvNet;

    @BindView(R.id.activity_my_main_iv_net_skin)
    ImageView activityMyMainIvSkin;
    @BindView(R.id.activity_main_ll_selector)
    LinearLayout activityMainLlSelector;
    @BindView(R.id.activity_main_rl_bg)
    RelativeLayout activityMainRlBg;
    @BindView(R.id.activity_main_fly_border_view)
    FlyBorderView activityMainFlyBorderView;
    @BindView(R.id.activity_main_my_fly_border_01)
    MyFlyBorderView activityMainMyFlyBorder01;
    @BindView(R.id.activity_main_my_fly_border_00)
    MyFlyBorderView activityMainMyFlyBorder00;
    @BindView(R.id.activity_main_my_fly_border_02)
    MyFlyBorderView activityMainMyFlyBorder02;
    @BindView(R.id.activity_main_my_fly_border_03)
    MyFlyBorderView activityMainMyFlyBorder03;

    private MainPersenter mMainPersenter = new MainPersenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //  Log.e("MainActivity", "onCreate: ===================== 123" );
        ButterKnife.bind(this);
        initDate();
    }

    private void initDate() {
        mMainPersenter.setOnHoverListener(activityMyMainLlMeeting, activityMyMainLlLocalVideo, activityMyMainLlLinCloud,
                activityMyMainLlLinSet, activityMainFlyBorderView);
        mMainPersenter.initSkinListener(activityMyMainIvSkin, this);
        // todo 每秒1秒格式化一次时间HH:mm
        mMainPersenter.initTime();
        // todo 格式化时间 MM月dd日
        mMainPersenter.formatDate(this, activityMainRlBg);

    }


    @OnClick({R.id.activity_my_main_ll_meeting, R.id.activity_my_main_ll_local_video,
            R.id.activity_my_main_ll_lin_cloud, R.id.activity_my_main_ll_lin_set,
            R.id.activity_my_main_iv_net_skin, R.id.activity_my_main_iv_network})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_my_main_ll_meeting:          //会议讨论
                mMainPersenter.onClickedRecordScreenLabeledPager();
                break;
            case R.id.activity_my_main_ll_local_video:      //本地视频
                mMainPersenter.onclickedLoaclVido();
                break;
            case R.id.activity_my_main_ll_lin_cloud:        //云空间
                mMainPersenter.onClickedLinClude();
                break;
            case R.id.activity_my_main_ll_lin_set:          //系统设置
                mMainPersenter.onclickedSystemSettings();
                break;
            case R.id.activity_my_main_iv_net_skin:    // 皮肤
                mMainPersenter.onclickedSkinPopupWindow(this, activityMyMainIvSkin);
                break;
            case R.id.activity_my_main_iv_network:  // 点击wifi
                mMainPersenter.onclickedToWiFi();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        final SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        int selector = mSharedPreferences.getInt("item_skin", 0); //todo 名称
        activityMainRlBg.setBackgroundResource(updateBg[selector]);
        mMainPersenter.showPersonalInformation(this);

    }

    @Override
    public void getDate() {
        long sysTime = System.currentTimeMillis();
        CharSequence sysTimeStr = DateFormat.format("MM月dd日", new Date());
        String day = DateUtil.getWeek(new Date());
        String space = "  ";
        activityMyMainTvDate.setText(sysTimeStr + space + day);
    }

    @Override
    public void toRecordScreenLabeledPager() {
        Intent intent = new Intent(MainActivity.this, RecordScreenLabeledActivity.class);
        startActivity(intent);
    }

    @Override
    public void toDialogPager(int pager) {
        Intent intent = new Intent(MainActivity.this, DialogActivity.class);
        intent.putExtra("Dialog_viewpage_index", pager);
        startActivity(intent);
    }

    @Override
    public void changeTime() {
        long sysTime = System.currentTimeMillis();
        CharSequence sysTimeStr = DateFormat.format("HH:mm", new Date());
        activityMyMainLlTvTime.setText(sysTimeStr);
    }

    @Override
    public void toLoaclVidoPager() {
        //startActivity(new Intent(MainActivity.this, VideoListActivity.class));
        startActivity(new Intent(MainActivity.this, TimeLineActivity.class));
    }

    @Override
    public void toLinCludePager() {
        startActivity(new Intent(MainActivity.this, CloudListActivity.class));
    }

    @Override
    public void toSystemSettingsPager() {
        startActivity(new Intent(MainActivity.this, SystemSettingsActivity.class));
    }

    @Override
    public void displayBox(int Type, View v) {
        switch (Type) {
            case 0://会议讨论
                activityMainMyFlyBorder00.attachToView(v, 1.05f, 1.05f);
                break;
            case 1:  //本地视频
                activityMainMyFlyBorder02.attachToView(v, 1.05f, 1.05f);
                break;
            case 2: //云空间
                activityMainMyFlyBorder01.attachToView(v, 1.00f, 1.05f);
                break;
            case 3: //系统设置
                activityMainMyFlyBorder03.attachToView(v, 1.05f, 1.05f);
                break;
        }

    }

    @Override
    public void hiddenBox(int Type, View v) {
        switch (Type) {
            case 0:
                activityMainMyFlyBorder00.hiddenToView();
                break;
            case 1:
                activityMainMyFlyBorder02.hiddenToView();
                break;
            case 2:
                activityMainMyFlyBorder01.hiddenToView();
                break;
            case 3:
                activityMainMyFlyBorder03.hiddenToView();
                break;
        }
    }

    @Override
    public void showName(String name) {
        activityMyMainName.setText(name);
        activityMyMainTvState.setText("已绑定云服务");
    }

    @Override
    public void showHeadPortrait(String url) {
        Glide.with(this).load(RBConfig.FILE_HOST_IP + url).placeholder(R.drawable.menu_head_defalt).error(R.drawable.menu_head_defalt).into(activityMyMainCivHead);
    }

    @Override
    public void toWiFiPager() {
        startActivity(new Intent(this,SettingNetWorkActivity.class));
    }

    @Override
    public void wiredNetworkHidden() {
        super.wiredNetworkHidden();
        activityMyMainIvNetWork.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.no_network));
        activityMyMainTvNet.setText(R.string.net_disconnect);
        //   有线网络断开提示框
        networkDisconnectPromptDailog();
    }

    @Override
    public void wiredNetworkShow() {
        super.wiredNetworkShow();
        activityMyMainIvNetWork.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.network));
        activityMyMainTvNet.setText(R.string.net_connect);
    }

    // todo   有线网络断开提示框
    private void networkDisconnectPromptDailog() {
    }

}
